import cv2 as cv


def imshow(windowName, img):
    # 创建一个窗口
    cv.namedWindow(windowName, cv.WINDOW_NORMAL)  # 使用cv2.WINDOW_NORMAL允许改变大小
    cv.imshow(windowName, img)
    # 调整窗口大小
    cv.resizeWindow(windowName, 1000, 600)  # 宽度800，高度600


img_color = cv.imread("image/test1.png")
img_template = cv.imread("image/test1sub.png")

# 获取模版图的宽高
height, width, s = img_template.shape

# template_res = cv.matchTemplate(img_color, img_template, cv.TM_SQDIFF_NORMED)
# minValue, maxValue, minIdx, maxIdx = cv.minMaxLoc(template_res)
# print(f"minValue:{minValue}, maxValue: {maxValue}, minIdx{minIdx}, maxIdx{maxIdx}")
# leftIdx = minIdx
# rightIdx = (minIdx[0] + width, minIdx[1] + height )


template_res = cv.matchTemplate(img_color, img_template, cv.TM_CCORR_NORMED)
minValue, maxValue, minIdx, maxIdx = cv.minMaxLoc(template_res)
print(f"minValue:{minValue}, maxValue: {maxValue}, minIdx{minIdx}, maxIdx{maxIdx}")
leftIdx = maxIdx
rightIdx = (maxIdx[0] + width, maxIdx[1] + height )


cv.rectangle(img_color, leftIdx, rightIdx, (0,0,255), 1)
imshow("rec_img", img_color)

cv.waitKey()
cv.destroyAllWindows()
